<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'c.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript">
	    function createXMLHttpRequest() {
	        try {
	            return new XMLHttpRequest();
	        } catch(e) {
	            try {
	                return new ActiveXObject("Msxml2.XMLHTTP");
	            } catch(e) {
	                try {
	                    return new ActiveXObject("Microsoft.XMLHTTP");
	                } catch(e) {
	                    alter("浏览器不兼容！");
	                    throw e;
	                }
	            }
	        }
	    }
	    
		window.onload = function () {
			var username = document.getElementById("username");
			username.onblur = function () {
				var xmlHttp = createXMLHttpRequest();
				xmlHttp.open("POST", "<c:url value='/ajax/ValidateServlet'/>", true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlHttp.send("username=" + username.value);
				xmlHttp.onreadystatechange = function () {
	    			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
						var content = xmlHttp.responseText;
						var error_username = document.getElementById("error_username");
						if (content == 0) {
							error_username.innerHTML = "用户名已被占用！";
						} else if (content == 1) {
							error_username.innerHTML = "恭喜，用户名可用！";
						}
					}
				};
			};
		};
	</script>
  </head>
  
  <body>
	<h1>AJAX 3</h1>
	<form action="" method="post">
		用户名：<input type="text" name="username" id="username"/><span id="error_username"></span><br/>
		密码：<input type="password" name="password"/><br/>
		<input type="submit" value="注册"/>
	</form>
  </body>
</html>
